import axios, { type AxiosInstance, type AxiosResponse, type InternalAxiosRequestConfig } from "axios";

axios.defaults.withCredentials = false;
let _baseURLUser;
if(import.meta.env.MODE === "development") {
  _baseURLUser = import.meta.env.VITE_API_URl;
} else if(import.meta.env.MODE === "production") {
  _baseURLUser = USER_GATEWAY ? '' : VUE_APP_BASE_API;
}

const ServerUrl: AxiosInstance = axios.create({
  baseURL: _baseURLUser,
  timeout: 120000
});

ServerUrl.interceptors.request.use(request => requestIntercept(request), error => errorIntercept(error));
ServerUrl.interceptors.response.use((response) => responseIntercept(response), error => errorIntercept(error));

//请求拦截
const requestIntercept = (request: InternalAxiosRequestConfig<any>) => {
  // request.headers!['Content-Type'] ='application/x-www-form-urlencoded';
  request.headers!['Content-Type'] ='application/json';

  return request;
}

const responseIntercept = (response: AxiosResponse) => {
  // console.log('response', response)
  if(response.request.responseType === 'blob') {
    return Promise.resolve(response);
  } else {
    return Promise.resolve(response.data);
  }
  
}
//错误拦截
const errorIntercept = (error: any) => {
  console.log("error___", error);
}


export { ServerUrl }